package sporteam.web.server;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sporteam.common.PMF;
import sporteam.web.client.AEuser;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;

@SuppressWarnings("serial")
public class UpdateUserInfo extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int age =   Integer.parseInt(req.getParameter("Age"));
		double weight = Double.parseDouble(req.getParameter("Weight"));
		String email = req.getParameter("Email");
		String gender = req.getParameter("Gender");
		
		String access_token = req.getParameter("facebook_access_token");
		
		
		FacebookClient facebookClient = new DefaultFacebookClient(
				access_token);	//establish connection with facebook-graph API with a specific access_token
		com.restfb.types.User facebook_user = facebookClient.fetchObject("me",
				com.restfb.types.User.class);	//me - the facebook user logged in ATM.
		PersistenceManager pm = PMF.get()
				.getPersistenceManager();
		AEuser user = pm.getObjectById(AEuser.class, facebook_user.getId());
		
		user.SetAge(age);
		user.SetGender(gender);
		user.SetWeight(weight);
		user.SetEmail(email);
		
		user.UpdateDate();
		pm.makePersistent(user);
		
		pm.close();
		
		super.doGet(req, resp);
	}
}
